﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace ppt_outliner
{
    public partial class ThisAddIn
    {
        private Form1 outlineForm;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                // 创建并显示大纲视图窗口
                outlineForm = new Form1();
                outlineForm.Show();

                // 注册 PowerPoint 事件
                Application.PresentationOpen += Application_PresentationOpen;
                Application.PresentationClose += Application_PresentationClose;
                Application.SlideSelectionChanged += Application_SlideSelectionChanged;
                Application.WindowSelectionChange += Application_WindowSelectionChange;

                // 只有在有活动演示文稿时才刷新树
                if (Application.Presentations.Count > 0 && Application.ActivePresentation != null)
                {
                    outlineForm.RefreshTree();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"插件初始化失败: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Application_PresentationOpen(PowerPoint.Presentation Pres)
        {
            try
            {
                if (outlineForm != null && !outlineForm.IsDisposed)
                {
                    outlineForm.RefreshTree();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理演示文稿打开事件失败: {ex.Message}");
            }
        }

        private void Application_PresentationClose(PowerPoint.Presentation Pres)
        {
            try
            {
                if (outlineForm != null && !outlineForm.IsDisposed)
                {
                    outlineForm.RefreshTree();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理演示文稿关闭事件失败: {ex.Message}");
            }
        }

        private void Application_SlideSelectionChanged(PowerPoint.SlideRange SldRange)
        {
            try
            {
                if (outlineForm != null && !outlineForm.IsDisposed)
                {
                    outlineForm.RefreshTree();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理幻灯片选择改变事件失败: {ex.Message}");
            }
        }

        private void Application_WindowSelectionChange(PowerPoint.Selection Sel)
        {
            try
            {
                if (outlineForm != null && !outlineForm.IsDisposed)
                {
                    outlineForm.RefreshTree();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理窗口选择改变事件失败: {ex.Message}");
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            try
            {
                // 取消事件订阅
                Application.PresentationOpen -= Application_PresentationOpen;
                Application.PresentationClose -= Application_PresentationClose;
                Application.SlideSelectionChanged -= Application_SlideSelectionChanged;
                Application.WindowSelectionChange -= Application_WindowSelectionChange;

                // 关闭大纲视图窗口
                if (outlineForm != null && !outlineForm.IsDisposed)
                {
                    outlineForm.Close();
                    outlineForm = null;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"插件关闭时清理失败: {ex.Message}");
            }
        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
